package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.BizProduct;
import com.ruoyi.system.domain.BizProductSeries;
import com.ruoyi.system.domain.vo.BizProductResponse;
import com.ruoyi.system.mapper.BizProductMapper;
import com.ruoyi.system.mapper.BizProductSeriesMapper;
import com.ruoyi.system.service.BizProductService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BizProductServiceImpl implements BizProductService {

    @Autowired
    private BizProductMapper productMapper;

    @Autowired
    private BizProductSeriesMapper productSeriesMapper;

    @Override
    public List<BizProductResponse> selectProductList(BizProduct product) {
        List<BizProduct> productList = productMapper.selectProductList(product);
        if (CollectionUtils.isEmpty(productList)) {
            return new ArrayList<>();
        }
        List<BizProductResponse> productReponseList = new ArrayList<>();
        for (BizProduct bizProduct : productList) {
            BizProductResponse productResponse = new BizProductResponse();
            BeanUtils.copyProperties(bizProduct, productResponse);
            BizProductSeries bizProductSeries = productSeriesMapper.selectProductSeriesById(bizProduct.getSeriesId());
            if (bizProductSeries != null) {
                productResponse.setSeriesName(bizProductSeries.getName());
            }
            BizProductSeries parentProductSeries = productSeriesMapper.selectProductSeriesById(bizProduct.getParentSeriesId());
            if (parentProductSeries != null) {
                productResponse.setParentSeriesName(parentProductSeries.getName());
            }
            productReponseList.add(productResponse);
        }
        return productReponseList;
    }

    @Override
    public BizProductResponse selectProductById(Long productId) {
        BizProduct bizProduct = productMapper.selectProductById(productId);
        if (bizProduct == null) {
            return null;
        }
        BizProductResponse productResponse = new BizProductResponse();
        BeanUtils.copyProperties(bizProduct, productResponse);
        BizProductSeries bizProductSeries = productSeriesMapper.selectProductSeriesById(bizProduct.getSeriesId());
        if (bizProductSeries != null) {
            productResponse.setSeriesName(bizProductSeries.getName());
        }
        BizProductSeries parentProductSeries = productSeriesMapper.selectProductSeriesById(bizProduct.getParentSeriesId());
        if (parentProductSeries != null) {
            productResponse.setParentSeriesName(parentProductSeries.getName());
        }
        return productResponse;
    }

    @Override
    public int insertProduct(BizProduct product) {
        return productMapper.insertProduct(product);
    }

    @Override
    public int updateProduct(BizProduct product) {
        return productMapper.updateProduct(product);
    }

    @Override
    public int deleteProductById(Long productId) {
        return productMapper.deleteProductById(productId);
    }

    @Override
    public int checkProductNameAndSeriesIdUnique(String name, Long seriesId, Long id) {
        return productMapper.checkProductNameAndSeriesIdUnique(name, seriesId, id);
    }

    @Override
    public int selectCount(BizProduct product) {
        return productMapper.selectCount(product);
    }
}
